<template>
  <div class="production-management">
    <page>
      <div slot="body">
        <div>
          <div class="line"></div>
          <el-form class="form" label-width="140px" inline>
            <!-- <el-form-item class="form-item" label="设计总需求量：" prop="name">
              <el-input v-model="total" placeholder="" disabled size="small" />
              <i class="el-icon-edit" @click="edit(1)"></i>
            </el-form-item>
            <el-form-item class="form-item" label="设计完成时间：" prop="name">
              <el-input v-model="time" placeholder="" disabled size="small" />
              <i class="el-icon-edit" @click="edit(2)"></i>
            </el-form-item> -->
          </el-form>
        </div>
       <!-- <div class="table-operation">
          <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd"> 添加 </el-button>
        </div> -->
        <el-table :data="tableData" border>
          <el-table-column label="序号" type="index" width="50px"></el-table-column>
          <el-table-column label="桁吊名称" prop="name"></el-table-column>
          <el-table-column label="预警" prop="monitor">
            <template slot-scope="scope">
              <el-switch
                :value="!!scope.row.monitor" @click.native.stop="changeMonitor(scope.row)">
              </el-switch>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="140px">
          <el-form-item label="设计总需求量：" prop="prodNum" v-if="num == 1">
            <el-input v-model="form.prodNum" placeholder="设计总需求量：" />
          </el-form-item>
          <el-form-item label="设计完成时间：" prop="finishDate" v-if="num == 2">
            <el-date-picker v-model="form.finishDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <el-dialog :title="prodTitle" :close-on-click-modal="false" :visible.sync="openProd" width="800px" append-to-body>
        <el-form ref="prodForm" :model="prodForm" :rules="prodRules" label-width="140px">
          <el-form-item label="物料名称：" prop="tunnel">
            <el-input v-model="prodForm.tunnel" placeholder="请输入物料名称" />
          </el-form-item>
          <el-form-item label="检验时间：" prop="steel">
            <el-input v-model="prodForm.steel" placeholder="请输入检验时间" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitProdForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <div slot="footer">
        <el-pagination
          background
          :page-size="pageVO.pageSize"
          :current-page.sync="pageVO.pageNum"
          :total="pageVO.total"
          :layout="pageOptions.layout"
          :page-sizes="pageOptions.pageSizes"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </page>
  </div>
</template>

<script>
import { getList, edit, deleteById, typeList } from '@/api/monitor';
import { getToken } from '@/utils/auth';
import pageQuery from '../../mixins/pageQuery';
export default {
  mixins: [pageQuery],
  data() {
    return {
      form: {
        prodNum: '',
        finishDate: '',
        id: '',
      },
      prodForm: {},
      open: false,
      openProd: false,
      total: '',
      time: '',
      title: '',
      prodTitle: '',
      tableData: [],
      areaList: [],
      statusOptions: [],
      unitList: [],
      num: 0,
      rules: {
        prodNum: [{ required: true, message: '设计总需求量不能为空', trigger: 'blur' }],
        finishDate: [{ required: true, message: '设计完成时间不能为空', trigger: 'blur' }],
      },
      prodRules: {
        tunnel: [{ required: true, message: '请输入物料名称', trigger: 'blur' }],
        tunnelArea: [{ required: true, message: '请选择隧道区域', trigger: 'chage' }],
        steel: [{ required: true, message: '请输入检验时间', trigger: 'blur' }],
        unit: [{ required: true, message: '请选择单元', trigger: 'chage' }],
        // upload:[
        //   { required: true, message: "", trigger: "chage" }
        // ]
      },
      uploadFileUrl: process.env.VUE_APP_BASE_API + '/steel-arch/client/file/upload', // 上传的图片服务器地址
      headers: {
        Authorization: 'Bearer ' + getToken(),
      },
      fileList: [],
    };
  },
  created() {
    this.getTableData();
    const { projectId } = this.$store.getters;
    this.prodForm.projectId = projectId;
    // getDicts({ dictType: 'sys_tunnel_area' }).then(res => {
    //   this.statusOptions = res.data.data;
    // });
    // typeList().then(res=>{
    // })
  },
  methods: {
    getTableData() {
      const { projectId } = this.$store.getters;
      const parmas = {
        projectId: projectId,
      };
      getList(parmas).then(res => {
        this.tableData = res.data.data;
        this.pageVO.total = res.data.total
      });
    },
    edit(num) {
      this.open = true;
      if (num == 1) {
        this.title = '设计总需求量修改';
        this.num = 1;
      } else {
        this.title = '设计完成时间修改';
        this.num = 2;
      }
    },
    changeMonitor(item){
      console.log(item)
      edit({
        id: item.id,
        monitor: (item.monitor == 0)?1:0
      }).then(response => {
        this.$message.success('修改成功');
        this.getTableData();
      });
    },
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const { projectId } = this.$store.getters;
          this.form.projectId = projectId;
          edit(this.form).then(response => {
            this.$message.success('修改成功');
            this.open = false;
            this.getTableData();
          });
        }
      });
    },
    submitProdForm: function () {
      this.$refs['prodForm'].validate(valid => {
        if (valid) {
          if (this.prodForm.id != null) {
            edit(this.prodForm).then(response => {
              this.$message.success('修改成功');
              this.openProd = false;
              this.getTableData();
            });
          } else {
            edit(this.prodForm).then(response => {
              this.$message.success('新增成功');
              this.openProd = false;
              this.getTableData();
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.$refs.prodForm.resetFields();
      this.openProd = false;
    },
    handleAdd() {
      this.openProd = true;
      this.prodTitle = '添加产品';
      this.prodForm={}
    },
    // 文件个数超出
    handleExceed() {
      this.$message.error(`只允许上传单个文件`);
    },
    // 上传失败
    handleUploadError(err) {
      this.$message.error('上传失败, 请重试');
    },
    // 上传成功回调
    handleUploadSuccess(res, file) {
      this.$message.success('上传成功');
      this.prodForm.image = res.data;
    },
    // 删除文件
    handleDelete(index) {
      //this.fileList.splice(index, 1);
    },
    handleEdit(row) {
      this.openProd = true;
      this.prodForm = row;
      const { projectId } = this.$store.getters;
      this.prodForm.projectId = projectId;
    },
    handleDelete(row) {
      this.$confirm('是否确认删除检验批次号为"' + row.prodCode + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return deleteById(row.id);
        })
        .then(() => {
          this.getTableData();
          this.$message.success('删除成功');
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.top {
  display: flex;
  justify-content: flex-start;
  margin: 20px 0;
}

.line {
  width: 8px;
  background: #1890ff;
  margin-right: 15px;
}
.form {
  display: flex;
  margin-top: 10px;
  ::v-deep {
    .el-form-item__label {
      font-weight: normal;
    }
  }
}
.form-item {
  position: relative;
}
.el-icon-edit {
  font-size: 20px;
  right: 10px;
  position: absolute;
  top: 8px;
}
</style>
